<?php
declare (strict_types = 1);

namespace app\listener;

use think\swoole\Websocket;
use think\swoole\websocket\Room;

class SwRoomMessage
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event,Websocket $ws,Room $room)
    {
        $roomid  = $event['room'];
        $text    = $event['text'];
        $fd      = $ws->getSender();
        $roomfds = $room->getClientList($roomid);
        if(!in_array($fd,$roomfds)){
            $ws->emit('roommessagecallback',"{$fd}不在房间{$roomid}中，无法发送消息");
            return;
        }

        $ws->to($roomfds)->emit('roommessagecallback',"{$fd}说：{$text}");
    }
}
